package cn.bvin.lib.app;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.bvin.lib.basic_support.R;
import cn.bvin.lib.interf.IActivityInit;
import cn.bvin.lib.utils.DeviceUtils;

public class WiseActivity extends ActionBarActivity implements IActivityInit
{
	private TextView tvActionBarTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// View actionbarLayout =
		// LayoutInflater.from(this).inflate(R.layout.custom_actionbar_layout,
		// null);
		// tvActionBarTitle = (TextView)
		// actionbarLayout.findViewById(R.id.custom_action_bar_title);
		// // 显示自定义视图
		// getSupportActionBar().setDisplayShowCustomEnabled(true);
		// getSupportActionBar().setCustomView(actionbarLayout, layout);
		getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		//
		// // 返回箭头（默认不显示）
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		// // 左侧图标点击事件使能
		// getSupportActionBar().setHomeButtonEnabled(true);
		// // 使左上角图标(系统)是否显示
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		// // 显示标题
		// getSupportActionBar().setDisplayShowTitleEnabled(false);

		AppManager.getAppManager().addActivity(this);
	}

	public void setActionBarTitle(CharSequence title)
	{
		if (tvActionBarTitle != null)
		{
			tvActionBarTitle.setText(title);
		}
	}

	@Override
	public void parserIntent()
	{

	}

	@Override
	public void initData()
	{

	}

	@Override
	public void initViews()
	{

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case android.R.id.home:
			if (!getSupportFragmentManager().popBackStackImmediate())
			{
				finish();
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		AppManager.getAppManager().finishActivity(this);
	}

	public int getWidth()
	{
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public void setActionBarTitle(boolean back, String textToSet)
	{
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		// getSupportActionBar().setHomeButtonEnabled(true);
		// Inflate the custom view
		LayoutInflater inflater = LayoutInflater.from(this);
		View header = inflater.inflate(R.layout.custom_actionbar_layout, null);

		// Here do whatever you need to do with the view (set text if it's a
		// textview or whatever)
		TextView tv = (TextView) header.findViewById(R.id.custom_action_bar_title);
		tv.setText(textToSet);

		// Magic happens to center it.
		int actionBarWidth = getWidth(); // Google for this method. Kinda easy.

		tv.measure(0, 0);
		int tvSize = tv.getMeasuredWidth();

		int leftSpace = 0;
		View homeButton = header.findViewById(R.id.home);
		homeButton.measure(0, 0);
		leftSpace = homeButton.getMeasuredWidth();
		if (!back)
		{
			homeButton.setVisibility(View.GONE);
		} else
		{
			homeButton.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					if (!getSupportFragmentManager().popBackStackImmediate())
					{
						finish();
					}
				}
			});

		}
		// 显示标题
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setCustomView(header);

		if (null != header)
		{
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();

			if (null != params)
			{
				int leftMargin = (actionBarWidth / 2 - leftSpace + DeviceUtils.dpToPx(getResources(), 62)) - (tvSize / 2);

				params.leftMargin = 0 >= leftMargin ? 0 : leftMargin;
			}
		}
	}

}
